package top.lshaci.learning.activiti.day2;

import cn.hutool.core.lang.Dict;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.Map;

import static top.lshaci.learning.activiti.util.ProcessEngineSingleton.singleton;

/**
 * ProcessVariableTest
 *
 * @author liangxu
 */
public class ProcessVariableTest {

    /**
     * 1、启动流程
     */
    @Test
    public void startProcess() {
        RuntimeService runtimeService = singleton.runtimeService();
        String processDefinitionKey = "LeaveFlow";
        Dict variable = Dict.create().set("剩余请假天数", "2天");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variable);
        System.err.println(processInstance);
    }

    /**
     * 2、设置流程变量
     */
    @Test
    public void setProcessVariable() {
        TaskService taskService = singleton.taskService();
        // 在办理过程中，添加流程变量
        Task task = taskService.createTaskQuery()
                .taskAssignee("张三经理")
                .singleResult();
//        taskService.setVariable(task.getId(), "请假人", "张三");
//        taskService.setVariable(task.getId(), "请假天数", 2);
//        Dict variables = Dict.create().set("请假时间", LocalDate.now());
//        taskService.setVariables(task.getId(), variables);
        // 在任务完成时，添加流程变量
        taskService.complete(task.getId(), Dict.create().set("回复", "好好耍"));
    }

    /**
     * 3、获取流程变量
     */
    @Test
    public void getProcessVariable() {
        TaskService taskService = singleton.taskService();
        Task task = taskService.createTaskQuery()
                .includeProcessVariables()
                .taskAssignee("张三经理")
                .singleResult();
        Map<String, Object> processVariables = task.getProcessVariables();
        System.err.println(processVariables);
        System.err.println(task.getTaskLocalVariables());
        Map<String, Object> variables = taskService.getVariables(task.getId());
        System.err.println(variables);
    }
}
